package com.lwc.controller;

import com.lwc.bean.puchare.Purchase;
import com.lwc.bean.puchare.Tt;
import com.lwc.common.bean.Page;
import com.lwc.common.bean.PageBean;
import com.lwc.common.bean.ResultBean;
import com.lwc.common.controller.BaseController;
import com.lwc.common.exceotion.ValidateException;
import com.lwc.service.PurchaseService;
import com.lwc.service.QueryPurchaseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "采购单管理")
@RequestMapping("/api")
public class PurchaseController extends BaseController {

    @Autowired
    PurchaseService purchaseService;
@Autowired
    QueryPurchaseService queryPurchaseService;
    @ApiOperation("新增采购单")
@PostMapping("/add")
    //保存采购单
public ResultBean save(@RequestBody  Purchase purchase){
int rs= purchaseService.save(purchase);
return rs==1?success("新增成功"):fail("新增失败");
    }


    @ApiOperation("查询采购单")
    @PostMapping("query1")
    public  ResultBean query(@RequestBody Tt tt){
 PageBean bean= queryPurchaseService.query(tt.toMap());
return  success("查询成功",bean);
    }

@ApiOperation("导出文档")
    @PostMapping("expose")
    public  ResultBean expose(@RequestBody Map map){
String url= purchaseService.expose(map);

        return success("导出成功",url);
    }

//删除
@ApiOperation("删除接口")
    @DeleteMapping("leave")
    public  ResultBean delete(int id){

        int rs=purchaseService.delete(id);
        return rs==1? success("删除成功"):fail("删除失败");
    }
//批量删除
@ApiOperation("批量删除接口")
@DeleteMapping("/manyleave")
public  ResultBean manyleave(@RequestParam Integer [] ids){
int rs= purchaseService.batchDelete(ids);
        return  rs>0?success("批量删除成功"):fail("删除失败");

    }
    @ApiOperation("采购单详情")
@GetMapping("/Purchase")
    public ResultBean get(int id){
        Purchase purchase=purchaseService.get(id);
return success("采购单详情",purchase);
}

@PutMapping("/Purchase")
@ApiOperation("更改采购单")
public  ResultBean update(@Validated  @RequestBody Purchase purchase, BindingResult result){
      validate(result);
        if (null==purchase.getPurchaseDetailList()||purchase.getPurchaseDetailList().size()==0){
            throw  new ValidateException("PurchaseDetailList","采购明细不能为空");
        }

        int rs =purchaseService.update(purchase);
        return rs>0?success("更改成功"):fail("更改失败");
}


}
